<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('organization.view') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
          <div class="mr10">
            <el-button
              v-if="isAuth('module:organization:export')"
              type="success"
              icon="Download"
              @click="exportHandle">
              {{ $t('common.export') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-card class="box-card" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.basic_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-image :src="dataForm.logo"></el-image>
                </el-col>
                <el-col :span="20">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('organization.organization_no')" label-align="right" :span="3">
                      {{ dataForm.organization_no }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.title')" label-align="right" :span="3">
                      {{ dataForm.title }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.token')" label-align="right" :span="3">
                      {{ dataForm.token }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.valid_time')" label-align="right" :span="3">
                      {{ dataForm.valid_time }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.create_time')" label-align="right" :span="3">
                      {{ dataForm.create_time }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('common.status')" label-align="right" :span="3">
                      {{ dataForm.status.text }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.detail_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-row>
                <el-col :span="4">
                  <el-image :src="dataForm.business_license"></el-image>
                </el-col>
                <el-col :span="20">
                  <el-descriptions border>
                    <el-descriptions-item width="200" :label="i18n('organization.manager_name')" label-align="right" :span="3">
                      {{ dataForm.manager_name }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.mobile')" label-align="right" :span="3">
                      {{ dataForm.mobile }}
                    </el-descriptions-item>
                    <el-descriptions-item width="200" :label="i18n('organization.address')" label-align="right" :span="3">
                      {{ dataForm.address }}
                    </el-descriptions-item>
                  </el-descriptions>
                </el-col>
              </el-row>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('organization.contact_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-descriptions border>
                <el-descriptions-item width="200" :label="i18n('organization.contact_name')" label-align="right">
                  {{ dataForm.contact_name }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.contact_mobile')" label-align="right">
                  {{ dataForm.contact_mobile }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.contact_email')" label-align="right">
                  {{ dataForm.contact_email }}
                </el-descriptions-item>

                <!-- 第二行 -->
                <el-descriptions-item width="200" :label="i18n('organization.contact_weixin')" label-align="right">
                  {{ dataForm.contact_weixin }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.contact_qq')" label-align="right" :span="2">
                  {{ dataForm.contact_qq }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>

          <el-card class="box-card mt10" shadow="never">
            <template v-slot:header>
              <div class="clearfix">
                <span>{{ i18n('common.audit_info') }}</span>
              </div>
            </template>
            <div class="text item">
              <el-descriptions border>
                <el-descriptions-item width="200" :label="i18n('common.audit_status')" label-align="right" :span="3">
                  {{ dataForm.audit_status.text }}
                </el-descriptions-item>
                <el-descriptions-item width="200" :label="i18n('organization.audit.content')" label-align="right" :span="3">
                  {{ dataForm.audit_content }}
                </el-descriptions-item>
              </el-descriptions>
            </div>
          </el-card>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-view">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('organization')
  const dataForm = reactive({
    id: 0,
    organization_no: '',
    logo: '',
    title: '',
    token: '',
    valid_time: '',
    audit_status: '',
    audit_content: '',
    create_time: '',
    status: '',
    business_license: '',
    manager_name: '',
    mobile: '',
    address: '',
    contact_name: '',
    contact_mobile: '',
    contact_email: '',
    contact_weixin: '',
    contact_qq: '',
    content: '',
  })

  const dataRule = reactive({})

  const {
    formRef
  } = useForm(model.value, dataForm)

  const init = (form) => {
    dataForm.id = query.id || 0

    nextTick(() => {
      form.value.resetFields()
      if (dataForm.id) {
        $http({
          url: $http.adornUrl(`/organization/view/${dataForm.id}`),
          method: 'get',
          params: $http.adornParams({}),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            dataForm.organization_no = data.data.organization_no
            dataForm.logo = data.data.logo
            dataForm.title = data.data.title
            dataForm.token = data.data.token
            dataForm.valid_time = data.data.valid_time
            dataForm.audit_status = data.data.audit_status
            dataForm.status = data.data.status
            dataForm.create_time = data.data.create_time
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/archive/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.business_license = data.data.business_license
                  dataForm.manager_name = data.data.manager_name
                  dataForm.mobile = data.data.mobile
                  dataForm.address = data.data.address
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/contact/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.contact_name = data.data.contact_name
                  dataForm.contact_mobile = data.data.contact_mobile
                  dataForm.contact_email = data.data.contact_email
                  dataForm.contact_weixin = data.data.contact_weixin
                  dataForm.contact_qq = data.data.contact_qq
                }
              }
            })
          }
        }).then(() => {
          if (dataForm.id) {
            $http({
              url: $http.adornUrl(`/${model.value}/audit/data`),
              method: 'get',
              params: $http.adornParams({
                'organization_id': dataForm.id
              }),
            }).then(({ data }) => {
              if (data && data.status === 200) {
                if(data.data) {
                  dataForm.audit_content = data.data.content
                }
              }
            })
          }
        })
      }
    })
  }


  const exportHandle = () => {
    $http({
      url: $http.adornUrl(`/organization/export`),
      method: 'post',
      data: $http.adornData({
        'id': dataForm.id
      })
    }).then(({data}) => {
      if (data && data.status === 200) {
        window.open(data.data)
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    init(formRef)
  })
</script>
